# -*- coding:utf-8 -*-
import threading

from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from Apps.UiTest.filters.uiTestCaseFilter import UiTestCaseFilter
from Apps.UiTest.models import UiTestCase
from Apps.UiTest.runTest.runUiTest import RunUiTest
from Apps.UiTest.ser.uiTestCaseSer import UiTestCaseSer
from Apps.common.fileUp.file import file_save
from Apps.common.response import SuccessResponse
import logging

logger = logging.getLogger("django")


class UiTestCaseView(ModelViewSet):
    queryset = UiTestCase.objects.all()
    serializer_class = UiTestCaseSer
    filter_class = UiTestCaseFilter

    authentication_classes = []

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        return SuccessResponse("OK")

    def perform_create(self, serializer):
        serializer.save()


class UiCaseFile(APIView):
    """保存Ui用例CASE接口"""

    def post(self, request):
        path = file_save(request, "uiCase")
        return SuccessResponse(path)


class RunUiTestCaseView(APIView):
    def post(self, request):
        # logger.info(request.data)
        threading.Thread(target=RunUiTest().run,
                         args=(request.data,)).start()

        return SuccessResponse("OK")
